package com.crossriver.crgis.portlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.WindowState;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crossriver.crgis.cofo.util.ApplicationActionUtil;
import com.crossriver.crgis.cofo.util.ApplicationCofOConstants;
import com.crossriver.crgis.cofo.util.ApplicationValidator;
import com.crossriver.crgis.cofo.util.CofORetrieveUtil;
import com.crossriver.crgis.cofo.util.Emailer;
import com.crossriver.crgis.database.model.Application_C_of_O;
import com.crossriver.crgis.database.model.Company_Contact_Person;
import com.crossriver.crgis.database.model.Documents;
import com.crossriver.crgis.database.model.Held_Plots;
import com.crossriver.crgis.database.model.History_Of_Information;
import com.crossriver.crgis.database.model.Contact_Information;
import com.crossriver.crgis.database.model.Individual_Information;
import com.crossriver.crgis.database.model.Organisation_Information;
import com.crossriver.crgis.database.model.Property_Information;
import com.crossriver.crgis.database.model.Supporting_Document;
import com.crossriver.crgis.database.model.impl.Application_C_of_OImpl;
import com.crossriver.crgis.database.model.impl.Contact_InformationImpl;
import com.crossriver.crgis.database.model.impl.DocumentsImpl;
import com.crossriver.crgis.database.model.impl.Held_PlotsImpl;
import com.crossriver.crgis.database.model.impl.Individual_InformationImpl;
import com.crossriver.crgis.database.model.impl.Organisation_InformationImpl;
import com.crossriver.crgis.database.model.impl.Property_InformationImpl;
import com.crossriver.crgis.database.service.Application_C_of_OLocalServiceUtil;
import com.crossriver.crgis.database.service.Company_Contact_PersonLocalServiceUtil;
import com.crossriver.crgis.database.service.DocumentsLocalServiceUtil;
import com.crossriver.crgis.database.service.Held_PlotsLocalServiceUtil;
import com.crossriver.crgis.database.service.History_Of_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Contact_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Individual_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Organisation_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Property_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Supporting_DocumentLocalServiceUtil;
import com.crossriver.crgis.database.service.impl.Held_PlotsLocalServiceImpl;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.bean.RendererUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.portlet.LiferayPortletURL;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.CalendarFactoryUtil;
import com.liferay.portal.kernel.util.DateUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.ListType;
import com.liferay.portal.service.LayoutLocalServiceUtil;
import com.liferay.portal.service.ListTypeServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.liferay.portlet.PortletURLUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class ApplicationCofO. This is the class used for
 * implementation of the ApplicationCofO portlet. This is based on Liferay
 * MVCPortlet and we are able to leverage on the MVC capabilities provided by
 * the MVC framework.
 * 
 * The general flow is that request from the portal hits the portlet, and the
 * appropriate method is called. We use the Utility class to get the domain
 * object that is in the request, we use domain validator to ensure that data is
 * clean, and act accordingly.
 * 
 * If there are validation errors, appropriate messages are sent to the view
 * component whicy is JSP. When no validation error, we make calls to the
 * service layer that handle the request from there.
 * 
 * @author Wole Adetiba
 * 
 */
public class ApplicationCofO extends MVCPortlet {

	// logger
	private static Log log = LogFactory.getLog(ApplicationCofO.class);

	/**
	 * This method is used for processing web request landing page of
	 * ApplicationCofO portlet. If there are errors, page is forwarded to the
	 * the landing page, otherwise, the page is forwarded to the Step 1 of
	 * either Individual or Organisation Application.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void pickApplicationType(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  pickApplicationType ");
		}
		Application_C_of_O application = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);

		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator.validateApplication_C_of_O(
				application, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ application.getApplicationId() + " <<<"
							+ application.getUserId());
					SessionErrors.add(actionRequest, error);
				}
			}

			return;
		}
		if (log.isInfoEnabled()) {
			log.info("No validation error for found for >>> "
					+ application.getApplicationId());
		}
		String appType = application.getApplicationType();
		String app = application.getApplication();
		if (null == appType || appType.equalsIgnoreCase("")) {

		} else {
			// generate the application no.

			application.setApplicationId(UUID.randomUUID().toString());
			application.setApplication(app);// this is newly added by wole
			
			
			application.setApplicationStartDate(CalendarFactoryUtil.getCalendar().getTime());
			if (log.isInfoEnabled()) {
				log.info("Set application Id to >>> "
						+ application.getApplicationId());
			}

			try {
				application = Application_C_of_OLocalServiceUtil
						.addApplication_C_of_O(application);
				if (log.isInfoEnabled()) {
					log.info("Application >>" + application.toString()
							+ " <<< successfully created.");
				}
			} catch (SystemException sys) {
				if (log.isDebugEnabled()) {
					log.debug("Creating Application >>"
							+ application.toString() + " <<< failed");
					log.debug("There is a problem persisting Application information "
							+ sys.getMessage());
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");

				SessionErrors.add(actionRequest, "application-error");
				sys.printStackTrace();

				return;
			}

			if (appType
					.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
				// go to individual page

				if (app.equalsIgnoreCase("CERTIFICATION") || app.equalsIgnoreCase("STATELAND")) {
					if (log.isInfoEnabled()) {
						log.info("Doing Certification ...");
					}
					String personalInformationJSP = "/html/application_cofo/individual/PersonalInformation.jsp";
					actionRequest.setAttribute("jspPage",
							personalInformationJSP);
					actionResponse.setRenderParameter("jspPage",
							personalInformationJSP);

					actionRequest.getPortletSession().setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					actionRequest.setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					// retrieve the information for the next stage
					if (log.isInfoEnabled()) {
						log.info("preparing for displaying of Information request "
								+ application.getApplicationId());
					}
					Individual_Information indInfo = ApplicationActionUtil
							.getIndividual_InformationFromRequest(actionRequest);

					indInfo.setApplicationId(application.getApplicationId());
					actionRequest.setAttribute(
							ApplicationCofOConstants.IND_INFO_SESSION_ATTR,
							indInfo);
				} else {
					if (log.isDebugEnabled()) {
						log.debug("Doing Recertification ...");
					}

					actionRequest.setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					String oldFileInformationJSP = "/html/application_cofo/common/OldFileInformation.jsp";
					actionRequest
							.setAttribute("jspPage", oldFileInformationJSP);
					actionResponse.setRenderParameter("jspPage",
							oldFileInformationJSP);

				}

				return;
			} else {
				// go to the organisation page
				if (app.equalsIgnoreCase("CERTIFICATION")|| app.equalsIgnoreCase("STATELAND")) {

					actionRequest.getPortletSession().setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					actionRequest.setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					// retrieve the organisation for the next stage
					log.info("preparing for displaying of organisation request "
							+ application.getApplicationId());

					Organisation_Information orgInfo = ApplicationActionUtil
							.getOrganisation_InformationFromRequest(actionRequest);

					orgInfo.setApplicationId(application.getApplicationId());

					actionRequest
							.setAttribute(
									ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
									orgInfo);

					String organisationInformationJSP = "/html/application_cofo/organisation/OrganisationInformation.jsp";
					actionRequest.setAttribute("jspPage",
							organisationInformationJSP);
					actionResponse.setRenderParameter("jspPage",
							organisationInformationJSP);

				} else {

					actionRequest.setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							application);

					String oldFileInformationJSP = "/html/application_cofo/common/OldFileInformation.jsp";

					actionRequest
							.setAttribute("jspPage", oldFileInformationJSP);
					actionResponse.setRenderParameter("jspPage",
							oldFileInformationJSP);

				}

				return;
			}

		}

	}

	public void editOldFileInfomation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editOldFileInfomation ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (log.isInfoEnabled()) {
			log.info("Set application Id to >>> " + applicationId);
		}
		Application_C_of_O application = null;
		Organisation_Information orgInfo = null;

		try {
			application = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(applicationId);
			if (log.isInfoEnabled()) {
				log.info("Application >>" + application.toString()
						+ " <<< successfully retreived.");
			}
		} catch (PortalException e) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application Information >>"
						+ applicationId + " <<< failed");
				log.debug("There is a problem retrieving Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();

			return;

		} catch (SystemException sys) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application Information >>"
						+ applicationId + " <<< failed");
				log.debug("There is a problem retrieving Application information "
						+ sys.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			sys.printStackTrace();

			return;
		}

		// added to replace old information by wole
		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, application);

		String oldFileInformationJSP = "/html/application_cofo/common/OldFileInformation.jsp";

		actionRequest.setAttribute("jspPage", oldFileInformationJSP);
		actionResponse.setRenderParameter("jspPage", oldFileInformationJSP);

	}

	public void saveOldFileInfomation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  saveOldFileInfomation ");
		}
		Application_C_of_O application = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);
		if (log.isInfoEnabled()) {
			log.info("Set application Id to >>> "
					+ application.getApplicationId());
		}

		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator.validateOldCofO(application,
				errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ application.getApplicationId() + " <<<"
							+ application.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String OldCofOJSP = "/html/application_cofo/common/OldFileInformation.jsp";
			actionRequest.setAttribute("jspPage", OldCofOJSP);
			actionResponse.setRenderParameter("jspPage", OldCofOJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
					application);

			return;
		}
		
		Application_C_of_O tempApp = null;
		try {
			// retrieve the old info, do update and save

			tempApp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(application.getApplicationId());

			tempApp.setOldCofONo(application.getOldCofONo());
			// tempApp.setApplication(application.getApplication()); commented
			// out by wole
			tempApp.setFileNo(application.getFileNo());

			boolean merge = true;
			tempApp = Application_C_of_OLocalServiceUtil
					.updateApplication_C_of_O(tempApp, merge);
			if (log.isInfoEnabled()) {
				log.info("Application >>" + application.toString()
						+ " <<< successfully created.");
			}
		} catch (PortalException sys) {
			if (log.isDebugEnabled()) {
				log.debug("Saving Application Information >>"
						+ application.toString() + " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ sys.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			sys.printStackTrace();

			return;
		} catch (SystemException sys) {
			if (log.isDebugEnabled()) {
				log.debug("Saving Application Information >>"
						+ tempApp.toString() + " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ sys.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			sys.printStackTrace();

			return;
		}
		// get the button clicked and behave accordingly 
		
		application = tempApp;
		
		
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String OldCofOJSP = "/html/application_cofo/common/OldFileInformation.jsp";
			actionRequest.setAttribute("jspPage", OldCofOJSP);
			actionResponse.setRenderParameter("jspPage", OldCofOJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
					application);
			
			SessionMessages.add(actionRequest, "file-information-save-success");

			return;
		}
		String applicationId = tempApp.getApplicationId();
		// retrieve the application, save and retrieve the next page,
		if ("ORGANISATION".equalsIgnoreCase(tempApp.getApplicationType())) {
			if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
				if (log.isDebugEnabled()) {
					log.debug("Can't Retrieve Organisation Infomation because ApplicationID is null or Blank");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");

				return;

			} else {
				Organisation_Information orgInfo = null;

				try {
					orgInfo = Organisation_InformationLocalServiceUtil
							.getOrganisation_Information(applicationId);
				} catch (PortalException e) {
					if (log.isInfoEnabled()) {
						log.info("Retrieving Organisation Information failed  >>"
								+ applicationId + " ");
						log.info("Doing New Organisation Information for >>>"
								+ applicationId + " ");
					}
					orgInfo = ApplicationActionUtil
							.getOrganisation_InformationFromRequest(actionRequest);

				} catch (SystemException e) {
					if (log.isInfoEnabled()) {
						log.info("Retrieving Organisation Information failed  >>"
								+ applicationId + " ");

						log.info("Doing New Organisation Information for >>>"
								+ applicationId + " ");
					}
					orgInfo = ApplicationActionUtil
							.getOrganisation_InformationFromRequest(actionRequest);

				}
				actionRequest.setAttribute(
						ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
						application);
				String organisationInformationJSP = "/html/application_cofo/organisation/OrganisationInformation.jsp";
				actionRequest.setAttribute("jspPage",
						organisationInformationJSP);
				actionResponse.setRenderParameter("jspPage",
						organisationInformationJSP);

				actionRequest
						.setAttribute(
								ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
								orgInfo);
			}
		} else {

			if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
				if (log.isDebugEnabled()) {
					log.debug("Can't Retrieve Personal Infomation because ApplicationID is null or Blank");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");

				return;

			} else {
				Individual_Information indInfo = null;

				try {
					indInfo = Individual_InformationLocalServiceUtil
							.getIndividual_Information(applicationId);
				} catch (PortalException e) {
					if (log.isInfoEnabled()) {
						log.info("Retrieving Individual Information failed  >>"
								+ applicationId + " ");

						log.info("Doing New Individual Information for >>>"
								+ applicationId + " ");
					}
					indInfo = ApplicationActionUtil
							.getIndividual_InformationFromRequest(actionRequest);

				} catch (SystemException e) {
					if (log.isInfoEnabled()) {
						log.info("Retrieving Individual Information failed  >>"
								+ applicationId + " ");

						log.info("Doing New Individual Information for >>>"
								+ applicationId + " ");
					}
					indInfo = ApplicationActionUtil
							.getIndividual_InformationFromRequest(actionRequest);

				}

				String personalInformationJSP = "/html/application_cofo/individual/PersonalInformation.jsp";
				actionRequest.setAttribute("jspPage", personalInformationJSP);
				actionResponse.setRenderParameter("jspPage",
						personalInformationJSP);

				actionRequest
						.setAttribute(
								ApplicationCofOConstants.IND_INFO_SESSION_ATTR,
								indInfo);
			}
		}

	}

	public void editOrganisationInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editOrganisationInformation ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Organisation Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Organisation_Information orgInfo = null;

			try {
				orgInfo = Organisation_InformationLocalServiceUtil
						.getOrganisation_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Organisation Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Organisation Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String organisationInformationJSP = "/html/application_cofo/organisation/OrganisationInformation.jsp";
			actionRequest.setAttribute("jspPage", organisationInformationJSP);
			actionResponse.setRenderParameter("jspPage",
					organisationInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
					orgInfo);
		}

		return;
	}

	public void editCompanyContactPerson(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editCompanyContactPerson ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Company Contact Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Company_Contact_Person coyContPerson = null;

			try {
				coyContPerson = Company_Contact_PersonLocalServiceUtil
						.getCompany_Contact_Person(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Coy Contact Person Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Coy Contact Person failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			// Retrieve contactInfo
			// retreive the state 2 information
			Contact_Information contactInfo = null;
			try {
				contactInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(coyContPerson
								.getApplicationId());
			} catch (PortalException e) {
				if (log.isInfoEnabled()) {
					log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
							+ coyContPerson.toString());
					log.info("Pick it from the request>>> This is a new one "
							+ coyContPerson.toString());
				}
				contactInfo = ApplicationActionUtil
						.getContact_InformationFromRequest(actionRequest);
			} catch (SystemException e) {
				if (log.isInfoEnabled()) {
					log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
							+ coyContPerson.toString());
					log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
							+ coyContPerson.toString());
					log.info("Pick it from the request>>> This is a new one "
							+ coyContPerson.toString());
				}
				contactInfo = ApplicationActionUtil
						.getContact_InformationFromRequest(actionRequest);
			}

			String companyContactPersonJSP = "/html/application_cofo/organisation/CompanyContactPerson.jsp";
			actionRequest.setAttribute("jspPage", companyContactPersonJSP);
			actionResponse.setRenderParameter("jspPage",
					companyContactPersonJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
					coyContPerson);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);

		}

		return;
	}

	public void editPersonalInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editPersonalInformation ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Personal Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Individual_Information indInfo = null;

			try {
				indInfo = Individual_InformationLocalServiceUtil
						.getIndividual_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Individual Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isInfoEnabled()) {
					log.info("Retrieving Individual Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String personalInformationJSP = "/html/application_cofo/individual/PersonalInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_INFO_SESSION_ATTR, indInfo);
		}

		return;
	}

	/**
	 * This method is evoked on the Step1 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 1 page, otherwise, the page is
	 * forwarded the Step 1 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getIndividual_InformationFromRequest of the
	 * Utility class, does some vaildation, and perists the address information
	 * using the Individual_InformationLocalServiceUtil utility that acts as a
	 * facade for the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void savePersonalInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  savePersonalInformation ");
		}
		Individual_Information indInf = ApplicationActionUtil
				.getIndividual_InformationFromRequest(actionRequest);

		// do validation and display right error messages
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator
				.validateIndividual_Information(indInf, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ indInf.getApplicationId() + " <<<"
							+ indInf.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String personalInformationJSP = "/html/application_cofo/individual/PersonalInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_INFO_SESSION_ATTR, indInf);

			return;
		}
		
		
		if (log.isInfoEnabled()) {
			log.info("No validation error for found for >>> "
					+ indInf.getApplicationId());
		}

		try {

			boolean merge = true;
			indInf = Individual_InformationLocalServiceUtil
					.updateIndividual_Information(indInf, merge);
			if (log.isInfoEnabled()) {
				log.info("Personal Information Successfully saved for >>> "
						+ indInf.getApplicationId());
				log.info("Personal Infomation >>" + indInf.toString());
			}
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + indInf.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
		


		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String personalInformationJSP = "/html/application_cofo/individual/PersonalInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_INFO_SESSION_ATTR, indInf);
			
			
			SessionMessages.add(actionRequest, "personal-information-save-success");
			return;
		}


		// retreive the state 2 information
		Contact_Information contactInfo = null;
		try {
			contactInfo = Contact_InformationLocalServiceUtil
					.getContact_Information(indInf.getApplicationId());
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ indInf.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ indInf.toString());
			}
			contactInfo = ApplicationActionUtil
					.getContact_InformationFromRequest(actionRequest);
		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ indInf.toString());
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ indInf.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ indInf.toString());
			}
			contactInfo = ApplicationActionUtil
					.getContact_InformationFromRequest(actionRequest);
		}

		// put a personal info in the request scope
		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_INFO_SESSION_ATTR, indInf);

		if (log.isInfoEnabled()) {
			// display contact information page
			log.info("Preparing for displaying  Contact Information for "
					+ indInf.toString());
		}
		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR, contactInfo);

		String addressInformationJSP = "/html/application_cofo/common/AddressInformation.jsp";
		actionRequest.setAttribute("jspPage", addressInformationJSP);
		actionResponse.setRenderParameter("jspPage", addressInformationJSP);

	}

	public void saveCompanyContactInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  saveCompanyContactInformation ");
		}
		Company_Contact_Person coyContPerson = ApplicationActionUtil
				.getCompany_Contact_PersonFromRequest(actionRequest);

		// do validation and display right error messages
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator
				.validateCompany_Contact_Person(coyContPerson, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isInfoEnabled()) {
					log.info("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ coyContPerson.getApplicationId() + " <<<"
							+ coyContPerson.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String coyContactPersonJSP = "/html/application_cofo/organisation/CompanyContactPerson.jsp";
			actionRequest.setAttribute("jspPage", coyContactPersonJSP);
			actionResponse.setRenderParameter("jspPage", coyContactPersonJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR,
					coyContPerson);

			return;
		}
		if (log.isInfoEnabled()) {
			log.info("No validation error for found for >>> "
					+ coyContPerson.getApplicationId());

		}
		try {

			boolean merge = true;
			coyContPerson = Company_Contact_PersonLocalServiceUtil
					.updateCompany_Contact_Person(coyContPerson, merge);
			if (log.isInfoEnabled()) {
				log.info("Company Contact Person  Successfully saved for >>> "
						+ coyContPerson.getApplicationId());
				log.info("Personal Infomation >>" + coyContPerson.toString());
			}
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + coyContPerson.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Coy Contact Person "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
		// generate an application id. this is done for the new application
		// indInf.setApplicationId(UUID.randomUUID().toString());

		// retreive the state 2 information
		Contact_Information contactInfo = null;
		try {
			contactInfo = Contact_InformationLocalServiceUtil
					.getContact_Information(coyContPerson.getApplicationId());
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ coyContPerson.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ coyContPerson.toString());
			}
			contactInfo = ApplicationActionUtil
					.getContact_InformationFromRequest(actionRequest);
		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ coyContPerson.toString());
				log.info("There is a problem retrieving Contact_InformationLocalServiceUtil "
						+ coyContPerson.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ coyContPerson.toString());
			}
			contactInfo = ApplicationActionUtil
					.getContact_InformationFromRequest(actionRequest);
		}

		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String coyContactPersonJSP = "/html/application_cofo/organisation/CompanyContactPerson.jsp";
			actionRequest.setAttribute("jspPage", coyContactPersonJSP);
			actionResponse.setRenderParameter("jspPage", coyContactPersonJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR,
					coyContPerson);

			
			SessionMessages.add(actionRequest, "company-contact-save-success");
			
			return;
		}

		// put a copy in the session and
		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_INFO_SESSION_ATTR, coyContPerson);
		if (log.isInfoEnabled()) {
			// display contact information page
			log.info("Preparing for displaying  Contact Information for "
					+ coyContPerson.toString());
		}
		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR, contactInfo);

		String addressInformationJSP = "/html/application_cofo/common/AddressInformation.jsp";
		actionRequest.setAttribute("jspPage", addressInformationJSP);
		actionResponse.setRenderParameter("jspPage", addressInformationJSP);

	}

	/**
	 * This method is evoked on the Step1 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 1 page, otherwise, the page is
	 * forwarded the Step 1 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getIndividual_InformationFromRequest of the
	 * Utility class, does some vaildation, and perists the address information
	 * using the Individual_InformationLocalServiceUtil utility that acts as a
	 * facade for the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void saveOrganisationInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  saveOrganisationInformation ");
		}
		Organisation_Information orgInf = ApplicationActionUtil
				.getOrganisation_InformationFromRequest(actionRequest);

		// do validation and display right error messages
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator
				.validateOrganisation_Information(orgInf, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isInfoEnabled()) {
					log.info("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ orgInf.getApplicationId() + " <<<"
							+ orgInf.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String organisationInformationJSP = "/html/application_cofo/organisation/OrganisationInformation.jsp";
			actionRequest.setAttribute("jspPage", organisationInformationJSP);
			actionResponse.setRenderParameter("jspPage",
					organisationInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
					orgInf);
			
			try {
				Application_C_of_O appCofO = Application_C_of_OLocalServiceUtil.
						getApplication_C_of_O(orgInf.getApplicationId());
				
				actionRequest.setAttribute(
						ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
						appCofO);
				
				
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return;
		}
		if (log.isInfoEnabled()) {
			log.info("No validation error found for >>> "
					+ orgInf.getApplicationId());
		}

		try {

			boolean merge = true;
			orgInf = Organisation_InformationLocalServiceUtil
					.updateOrganisation_Information(orgInf, merge);
			if (log.isInfoEnabled()) {
				log.info("Personal Information Successfully saved for >>> "
						+ orgInf.getApplicationId());
				log.info("Personal Infomation >>" + orgInf.toString());
			}
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + orgInf.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Organisation information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
		// generate an application id. this is done for the new application
		// indInf.setApplicationId(UUID.randomUUID().toString());


		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String organisationInformationJSP = "/html/application_cofo/organisation/OrganisationInformation.jsp";
			actionRequest.setAttribute("jspPage", organisationInformationJSP);
			actionResponse.setRenderParameter("jspPage",
					organisationInformationJSP);
			
			try {
				Application_C_of_O appCofO = Application_C_of_OLocalServiceUtil.
						getApplication_C_of_O(orgInf.getApplicationId());
				
				actionRequest.setAttribute(
						ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
						appCofO);
				
				
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			actionRequest.setAttribute(
					ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
					orgInf);
			
			SessionMessages.add(actionRequest, "organisation-information-save-success");

			return;
		}

		
		// retreive the state 2 information
		Company_Contact_Person coyContactPerson = null;
		try {
			coyContactPerson = Company_Contact_PersonLocalServiceUtil
					.getCompany_Contact_Person(orgInf.getApplicationId());
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Company_Contact_Person "
						+ orgInf.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ orgInf.toString());
			}
			coyContactPerson = ApplicationActionUtil
					.getCompany_Contact_PersonFromRequest(actionRequest);
		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("There is a problem retrieving Company_Contact_Person "
						+ orgInf.toString());
				log.info("Pick it from the request>>> This is a new one "
						+ orgInf.toString());
			}
			coyContactPerson = ApplicationActionUtil
					.getCompany_Contact_PersonFromRequest(actionRequest);
		}

		// put a copy in the session and
		actionRequest.setAttribute(
				ApplicationCofOConstants.COY_INFO_SESSION_ATTR, orgInf);

		if (log.isInfoEnabled()) {
			// display contact information page
			log.info("Preparing for displaying  Contact Information for "
					+ orgInf.toString());
		}
		actionRequest.setAttribute(
				ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR,
				coyContactPerson);

		String coyContactPersonJSP = "/html/application_cofo/organisation/CompanyContactPerson.jsp";
		actionRequest.setAttribute("jspPage", coyContactPersonJSP);
		actionResponse.setRenderParameter("jspPage", coyContactPersonJSP);
	}

	public void editAddressInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editAddressInformation ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Personal Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Contact_Information addressInfo = null;

			try {
				addressInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Individual Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Contact Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String addressInformationJSP = "/html/application_cofo/common/AddressInformation.jsp";
			actionRequest.setAttribute("jspPage", addressInformationJSP);
			actionResponse.setRenderParameter("jspPage", addressInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					addressInfo);
		}

		return;
	}

	/**
	 * This method is evoked on the Step2 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 2 page, otherwise, the page is
	 * forwarded the Step 2 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getContact_InformationFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Contact_InformationLocalServiceUtil utility that acts as a facade for
	 * the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void saveAddressInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  saveAddressInformation ");
		}
		Contact_Information contactInfo = ApplicationActionUtil
				.getContact_InformationFromRequest(actionRequest);

		// do validation and display right error messages
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator.validateAddress_Information(
				contactInfo, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ contactInfo.getApplicationId() + " <<<"
							+ contactInfo.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String personalInformationJSP = "/html/application_cofo/common/AddressInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);

			return;
		}

		if (log.isInfoEnabled()) {
			log.info("successfully retrieved  Contact_Information from the web >>> "
					+ contactInfo.toString());
		}
		// persist the address information in the database.
		try {
			// check if the info already exists
			try {
				Contact_Information tempContactInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(contactInfo.getApplicationId());

				//
				contactInfo.setEmailAddress(tempContactInfo.getEmailAddress());
				contactInfo.setFax(tempContactInfo.getFax());
				// contactInfo.setPoBox(tempContactInfo.getPoBox());
				contactInfo.setPhoneMobile(tempContactInfo.getPhoneMobile());
				contactInfo.setPhoneOffice(tempContactInfo.getPhoneOffice());
				contactInfo.setPhoneHome(tempContactInfo.getPhoneHome());

				if (log.isInfoEnabled()) {
					log.info("Successfully synced before tring update >>>"
							+ contactInfo.toString());
				}

			} catch (PortalException e) {
				if (log.isInfoEnabled()) {
					log.info("New Contact_Information info >>>"
							+ e.getMessage() + contactInfo.toString()
							+ " <<< failed");
				}
			}

			if (log.isInfoEnabled()) {
				log.info("About Saving Address portion of the contact information");
			}

			boolean merge = true;
			contactInfo = Contact_InformationLocalServiceUtil
					.updateContact_Information(contactInfo, merge);

			if (log.isInfoEnabled()) {
				log.info("Address Information Successfully saved. >>>"
						+ contactInfo.toString());
			}

		} catch (SystemException e) {

			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + contactInfo.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}

			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}

		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String personalInformationJSP = "/html/application_cofo/common/AddressInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);
			
			SessionMessages.add(actionRequest, "address-information-save-success");


			return;
		}

		if (log.isInfoEnabled()) {
			log.info("Preparing for displaying the contact information  ");
		}
		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR, contactInfo);

		String contactInformationJSP = "/html/application_cofo/common/ContactInformation.jsp";
		actionRequest.setAttribute("jspPage", contactInformationJSP);
		actionResponse.setRenderParameter("jspPage", contactInformationJSP);

	}

	public void editContactInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isDebugEnabled()) {
			log.debug("In  editContactInformation ");
		}

		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Personal Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Contact_Information addressInfo = null;

			try {
				addressInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Individual Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Contact Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String contactInformationJSP = "/html/application_cofo/common/ContactInformation.jsp";
			actionRequest.setAttribute("jspPage", contactInformationJSP);
			actionResponse.setRenderParameter("jspPage", contactInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					addressInfo);
		}

		return;
	}

	/**
	 * This method is evoked on the Step3 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 3 page, otherwise, the page is
	 * forwarded the Step 4 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getContact_InformationFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Contact_InformationLocalServiceUtil utility that acts as a facade for
	 * the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void saveContactInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  saveContactInformation ");
		}
		Contact_Information contactInfo = ApplicationActionUtil
				.getContact_InformationFromRequest(actionRequest);
		if (log.isInfoEnabled()) {
			log.info("successfully retrieved  Contact_Information from the web >>> "
					+ contactInfo.toString());
		}
		// retrieve the old value from session or database and merge the contact
		// information, then save into the database.

		// do validation and display right error messages
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator.validateContact_Information(
				contactInfo, errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isInfoEnabled()) {
					log.info("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ contactInfo.getApplicationId() + " <<<"
							+ contactInfo.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			String personalInformationJSP = "/html/application_cofo/common/ContactInformation.jsp";
			actionRequest.setAttribute("jspPage", personalInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", personalInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);

			return;
		}

		// String contactInformationJSP =
		// "/html/application_cofo/common/ContactInformation.jsp";
		// actionRequest.setAttribute("jspPage", contactInformationJSP);
		// actionResponse.setRenderParameter("jspPage", contactInformationJSP);

		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR, contactInfo);

		Contact_Information tempContactInfo = contactInfo;

		try {
			if (log.isInfoEnabled()) {
				log.info("Retrieving address information before the contact update is done  ");
			}
			tempContactInfo = Contact_InformationLocalServiceUtil
					.getContact_Information(contactInfo.getApplicationId());

			tempContactInfo.setPhoneMobile(contactInfo.getPhoneMobile());
			tempContactInfo.setPhoneOffice(contactInfo.getPhoneOffice());
			tempContactInfo.setPhoneHome(contactInfo.getPhoneHome());

			tempContactInfo.setEmailAddress(contactInfo.getEmailAddress());
			tempContactInfo.setFax(contactInfo.getFax());

			//tempContactInfo.setPoBox(contactInfo.getPoBox());
			// tempContactInfo.set)(contactInfo.getPoBox());
			if (log.isInfoEnabled()) {
				log.info("State of Contact_Information after update >>> "
						+ tempContactInfo.toString());
			}
			boolean merge = true;
			Contact_InformationLocalServiceUtil.updateContact_Information(
					tempContactInfo, merge);
			if (log.isInfoEnabled()) {
				log.info("Contact information is successfully updated. >>> "
						+ tempContactInfo.toString());
			}
			// CHECK HERE PLS

			// get the button clicked and behave accordingly
			String buttonClicked = ParamUtil.getString(actionRequest,
					"buttonClicked");
			// System.out.print("Button Clicked Name>>>" + buttonClicked);
			if (buttonClicked.equalsIgnoreCase("btnSave")) {
				String personalInformationJSP = "/html/application_cofo/common/ContactInformation.jsp";
				actionRequest.setAttribute("jspPage", personalInformationJSP);
				actionResponse.setRenderParameter("jspPage",
						personalInformationJSP);

				actionRequest.setAttribute(
						ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
						contactInfo);

				SessionMessages.add(actionRequest, "contact-information-save-success");

				
				return;
			}
		} catch (PortalException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Contact_Information >>"
						+ tempContactInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Contact_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;

		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Contact_Information >>"
						+ tempContactInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Contact_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
			
		
		if (log.isInfoEnabled()) {
			log.info("Preparing for displaying the application history page  ");
		}
		List<History_Of_Information> appHistory = null;

		try {
			appHistory = History_Of_InformationLocalServiceUtil
					.getHistory_Of_InformationByApplId(contactInfo
							.getApplicationId());

			contactInfo = Contact_InformationLocalServiceUtil
					.getContact_Information(contactInfo.getApplicationId());
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Retrieving Application History failed  >>"
						+ contactInfo.getApplicationId() + " ");
				log.info(" Application History This is a new one "
						+ contactInfo.getApplicationId());
			}
		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Retrieving Application History failed  >>"
						+ contactInfo.getApplicationId() + " ");
				log.info(" Application History This is a new one "
						+ contactInfo.getApplicationId());
			}
		}

		
		
		
		//chrck the type of application
		Application_C_of_O applictn = null;
		
		try {
			applictn = Application_C_of_OLocalServiceUtil.
					getApplication_C_of_O(contactInfo.getApplicationId());
		} catch (PortalException e1) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application C of O failed  >>>");
			}
			
		} catch (SystemException e1) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application C of O failed  >>>");
			}
		}
		
		
		actionRequest.setAttribute(
				ApplicationCofOConstants.HISTORY_INFO_SESSION_ATTR, appHistory);

		actionRequest.setAttribute(
				ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR, contactInfo);
		
		
		String applicationT = applictn.getApplication();
		
		System.out.println("application--->>>" + applicationT);
		if ("stateland".equalsIgnoreCase(applicationT)){
			Property_Information propInfo=null;
			try {
				propInfo = Property_InformationLocalServiceUtil
				.getProperty_Information(contactInfo.getApplicationId());

			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Property Information failed  >>>");
				}
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Property Information failed  >>>");
				}
			}
			List<Held_Plots> heldPlots = null;
			
			//heldPlots = ApplicationActionUtil.getHeldPlots(actionRequest);
			 DynamicQuery query = DynamicQueryFactoryUtil.forClass(Held_Plots.class)
			   .add(PropertyFactoryUtil.forName("applicationId").eq(new String(contactInfo.getApplicationId())));
			 
			 try {
				 heldPlots=	Held_PlotsLocalServiceUtil.dynamicQuery(query);
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			actionRequest.setAttribute("heldPlots", heldPlots);
			
			if(propInfo == null){
				propInfo = new Property_InformationImpl();
			}
			
			if(applictn == null){
				applictn = new Application_C_of_OImpl();
			}
			if(contactInfo == null){
				contactInfo = new Contact_InformationImpl();
			}
			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR, propInfo);

			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, applictn);
			
			String applicationHistoryJSP = "/html/application_cofo/stateland/PropertyInformation.jsp";
			actionRequest.setAttribute("jspPage", applicationHistoryJSP);
			actionResponse.setRenderParameter("jspPage", applicationHistoryJSP);

			
		}
		else
		{
			String applicationHistoryJSP = "/html/application_cofo/common/ApplicationHistory.jsp";
			actionRequest.setAttribute("jspPage", applicationHistoryJSP);
			actionResponse.setRenderParameter("jspPage", applicationHistoryJSP);
		}
		

	}

	public void editApplicationHistory(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editApplicationHistory ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Application History because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			List<History_Of_Information> appHistory = null;
			Contact_Information contactInfo = null;

			try {
				appHistory = History_Of_InformationLocalServiceUtil
						.getHistory_Of_InformationByApplId(applicationId);

				contactInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application History failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application History failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String applicationHistoryJSP = "/html/application_cofo/common/ApplicationHistory.jsp";
			actionRequest.setAttribute("jspPage", applicationHistoryJSP);
			actionResponse.setRenderParameter("jspPage", applicationHistoryJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.HISTORY_INFO_SESSION_ATTR,
					appHistory);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);

		}

		return;
	}

	/**
	 * This method is evoked on the Step4 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 4 page, otherwise, the page is
	 * forwarded the Step 5 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getProperty_InformationFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the History_Of_InformationLocalServiceUtil utility that acts as a facade
	 * for the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void saveApplicationHistory(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In saveApplicationHistory 1:: ");
		}
		// TODO
		Application_C_of_O appCofO = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);

		Property_Information propInfo = ApplicationActionUtil
				.getProperty_InformationFromRequest(actionRequest);

		// String applicationHistoryJSP =
		// "/html/application_cofo/common/ApplicationHistory.jsp";
		// actionRequest.setAttribute("jspPage", applicationHistoryJSP);
		// actionResponse.setRenderParameter("jspPage", applicationHistoryJSP);

		String applicationId = propInfo.getApplicationId();

		List<History_Of_Information> history = ApplicationActionUtil
				.getHistory_Of_InformationFromRequest(actionRequest);

		// In other to be able to take care of update and edit
		// we need to delete the existing ones and save all entries as new

		try {
			History_Of_InformationLocalServiceUtil
					.deleteHistory_Of_InformationByApplId(propInfo
							.getApplicationId());
		} catch (SystemException e1) {
			if (log.isDebugEnabled()) {
				log.debug("Deleting History_Of_Information >>"
						+ propInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting History_Of_Information information "
						+ e1.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e1.printStackTrace();
			return;
		}

		for (Iterator iterator = history.iterator(); iterator.hasNext();) {
			History_Of_Information history_Of_Information2 = (History_Of_Information) iterator
					.next();
			if (log.isInfoEnabled()) {
				log.info("successfully retrieved  History_Of_Information from the web >>> "
						+ history_Of_Information2.toString()
						+ "<<< "
						+ propInfo.getApplicationId());
			}
			try {

				History_Of_InformationLocalServiceUtil
						.addHistory_Of_Information(history_Of_Information2);
				if (log.isInfoEnabled()) {
					log.info("History Information successfully updated. >>> "
							+ history_Of_Information2.toString());
				}
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Creating History_Of_Information >>"
							+ history_Of_Information2.toString()
							+ " <<< failed");
					log.debug("There is a problem persisting History_Of_Information information "
							+ e.getMessage());
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");

				SessionErrors.add(actionRequest, "application-error");
				e.printStackTrace();
				return;
			}

		}

		// CHECK HERE PLS
		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {

			List<History_Of_Information> appHistory = null;
			Contact_Information contactInfo = null;

			try {
				appHistory = History_Of_InformationLocalServiceUtil
						.getHistory_Of_InformationByApplId(applicationId);

				for (Iterator iterator = appHistory.iterator(); iterator
						.hasNext();) {
					History_Of_Information history_Of_Information = (History_Of_Information) iterator
							.next();
					System.out.println("old app id>>>"
							+ history_Of_Information.getOldApplicationId());
				}
				System.out.println("applicationId>>>" + applicationId);
				contactInfo = Contact_InformationLocalServiceUtil
						.getContact_Information(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application History failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application History failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}

			String applicationHistoryJSP = "/html/application_cofo/common/ApplicationHistory.jsp";
			actionRequest.setAttribute("jspPage", applicationHistoryJSP);
			actionResponse.setRenderParameter("jspPage", applicationHistoryJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.HISTORY_INFO_SESSION_ATTR,
					appHistory);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					contactInfo);
			
			SessionMessages.add(actionRequest, "application-history-save-success");

			return;
		}

		// try {
		// propInfo =
		// Property_InformationLocalServiceUtil.getProperty_Information(applicationId);
		// } catch (PortalException e2) {
		// if (log.isInfoEnabled()){
		// log.info("Error retrieving property info >>" +e2.getMessage() +
		// " <<< failed");
		// }
		// } catch (SystemException e2) {
		// if (log.isInfoEnabled()){
		// log.info("Error retrieving property info >>" +e2.getMessage() +
		// " <<< failed");
		// }
		//
		// log.info("successfully retrieved  Application information from the web >>> "
		// + propInfo.toString() +"<<< " + propInfo.getApplicationId() );
		// }

		try {
			propInfo = Property_InformationLocalServiceUtil
					.getProperty_Information(applicationId);

			appCofO = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(applicationId);

		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("New Property Information  >>" + applicationId + " ");
			}

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.debug("New Property Information  >>" + applicationId + " ");
			}

			if (log.isDebugEnabled()) {
				log.debug("Preparing to navigate to Step 5 of the application >>"
						+ " Property Information page " + propInfo.toString());
			}
		}
		
		actionRequest.setAttribute(
				ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR, propInfo);

		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, appCofO);
		
		String propertyInformationJSP = "";
		
		if("State Land".equalsIgnoreCase(appCofO.getApplication())){
			propertyInformationJSP = "/html/application_cofo/stateland/PropertyInformation.jsp";
		}else{
			propertyInformationJSP = "/html/application_cofo/common/PropertyInformation.jsp";
		}
		
		
		actionRequest.setAttribute("jspPage", propertyInformationJSP);
		actionResponse.setRenderParameter("jspPage", propertyInformationJSP);
		
		
		
		return;
	}

	public void editPropertyInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		if (log.isInfoEnabled()) {
			log.info("In  editPropertyInformation ");
		}
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (null == applicationId || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Can't Retrieve Property Infomation because ApplicationID is null or Blank");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;

		} else {
			Property_Information propertyInfo = null;
			Application_C_of_O appCofO = null;
			try {
				propertyInfo = Property_InformationLocalServiceUtil
						.getProperty_Information(applicationId);

				appCofO = Application_C_of_OLocalServiceUtil
						.getApplication_C_of_O(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Property Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Property Information failed  >>"
							+ applicationId + " ");
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				e.printStackTrace();

				return;
			}
			String propertyInformationJSP =null;
			if ("stateland".equalsIgnoreCase(appCofO.getApplication())){
				List<Held_Plots> heldPlots = null;
				
				//heldPlots = ApplicationActionUtil.getHeldPlots(actionRequest);
				 DynamicQuery query = DynamicQueryFactoryUtil.forClass(Held_Plots.class)
				   .add(PropertyFactoryUtil.forName("applicationId").eq(new String(appCofO.getApplicationId())));
				 
				 try {
					 heldPlots=	Held_PlotsLocalServiceUtil.dynamicQuery(query);
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				actionRequest.setAttribute("heldPlots", heldPlots);
				 propertyInformationJSP = "/html/application_cofo/stateland/PropertyInformation.jsp";
			}else
			{
				propertyInformationJSP = "/html/application_cofo/common/PropertyInformation.jsp";	
			}
			
			actionRequest.setAttribute("jspPage", propertyInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", propertyInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
					propertyInfo);

			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, appCofO);

		}

		return;
	}

	/**
	 * This method is evoked on the Step5 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 5 page, otherwise, the page is
	 * forwarded the Step 6 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getProperty_InformationFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Property_InformationLocalServiceUtil utility that acts as a facade
	 * for the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void savePropInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		if (log.isInfoEnabled()) {
			log.info("In  savePropInformation");
		}

		Property_Information propInfo = ApplicationActionUtil
				.getProperty_InformationFromRequest(actionRequest);
		
		// this is retrieved for use in later stage
		Application_C_of_O application = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);


		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator
				.validateApplication_PropertyInformation(propInfo, errors);

		if(null == application.getLandUseType() || 
				"".equalsIgnoreCase(application.getLandUseType())){
			errors.add("land-use-is-mandatory");
			foundError = true;
		}
		
		if(null == application.getLandUsePurpose() || 
				"".equalsIgnoreCase(application.getLandUsePurpose())){
			errors.add("use-purpose-is-mandatory");
			foundError = true;
		}
		
		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ propInfo.getApplicationId() + " <<<"
							+ propInfo.getUserId());
				}

				SessionErrors.add(actionRequest, error);
				String PropertyInformationJSP = "/html/application_cofo/common/PropertyInformation.jsp";
				actionRequest.setAttribute("jspPage", PropertyInformationJSP);
				actionResponse.setRenderParameter("jspPage",
						PropertyInformationJSP);

				actionRequest.setAttribute(
						ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
						propInfo);
				
				actionRequest.setAttribute(
						ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
						application);

			}

			return;
		}

		if (log.isInfoEnabled()) {
			log.info("successfully retrieved  Property_Information from the web >>> "
					+ propInfo.toString()
					+ "<<< "
					+ propInfo.getApplicationId());
		}
	
		Application_C_of_O applicationTemp = null;
		try {

			if (log.isInfoEnabled()) {
				log.info("successfully saved  Property_Information from the web >>> "
						+ propInfo.toString()
						+ "<<< "
						+ propInfo.getApplicationId());
			}
			// retrieve appl info and do update
			applicationTemp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(application.getApplicationId());

			applicationTemp.setLandUsePurpose(application.getLandUsePurpose());
			applicationTemp.setLandUseType(application.getLandUseType());

			boolean merge = true;
			propInfo = Property_InformationLocalServiceUtil
					.updateProperty_Information(propInfo, merge);

			Application_C_of_OLocalServiceUtil.updateApplication_C_of_O(
					applicationTemp, merge);
			// then commiyt
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.debug("Creating Property_Information >>"
						+ propInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Property_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Property_Information >>"
						+ propInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Property_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String propertyInformationJSP = "/html/application_cofo/common/PropertyInformation.jsp";
			actionRequest.setAttribute("jspPage", propertyInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", propertyInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
					propInfo);
			
			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
					applicationTemp);

			SessionMessages.add(actionRequest, "property-information-save-success");
			
			return;
		}
		List<Supporting_Document> suppDoc = null;

		actionRequest.setAttribute(
				ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR, propInfo);

		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
				applicationTemp);

		// get the supporting info from db
		try {
			suppDoc = Supporting_DocumentLocalServiceUtil
					.getSupporting_DocumentByApplId(application
							.getApplicationId());

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("New Supporting Documents for >>"
						+ applicationTemp.getApplicationId() + " ");
			}
		}

		if (suppDoc == null || suppDoc.size() == 0) {
			suppDoc = ApplicationActionUtil
					.getSupporting_DocumentInput(applicationTemp);
		}

		actionRequest.setAttribute("suppDoc", suppDoc);

		if (log.isInfoEnabled()) {
			log.info("Preparing to navigate to Step 6 of the application >>"
					+ " Supporting Document  page "
					+ propInfo.getApplicationId());
		}

		String supportingDocumentJSP = "/html/application_cofo/common/SupportingDocuments.jsp";
		actionRequest.setAttribute("jspPage", supportingDocumentJSP);
		actionResponse.setRenderParameter("jspPage", supportingDocumentJSP);

	}

	
	public void savePropInformationStateLand(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		if (log.isInfoEnabled()) {
			log.info("In  savePropInformation");
		}

		Property_Information propInfo = ApplicationActionUtil
				.getProperty_InformationFromRequest(actionRequest);
		
		// this is retrieved for use in later stage
		Application_C_of_O application = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);
		
		
		List<Held_Plots> heldPlots = null;
		
		
		heldPlots = ApplicationActionUtil.getHeldPlots(actionRequest);
		
		
		
		
		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator
				.validateApplication_PropertyInformationStateLand(propInfo, errors);
		boolean foundError2  = ApplicationValidator
		.validateApplication_PropertyInformationStateLandHeldPlots(heldPlots, errors);
		
		if (true==foundError2){
			foundError=true;
		}
		
		if(null == application.getLandUseType() || 
				"".equalsIgnoreCase(application.getLandUseType())){
			errors.add("land-use-is-mandatory");
			foundError = true;
		}
		
		if(null == application.getLandUsePurpose() || 
				"".equalsIgnoreCase(application.getLandUsePurpose())){
			errors.add("use-purpose-is-mandatory");
			foundError = true;
		}
		
		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ propInfo.getApplicationId() + " <<<"
							+ propInfo.getUserId());
				}

				SessionErrors.add(actionRequest, error);
				
			}
			
			String PropertyInformationJSP = "/html/application_cofo/stateland/PropertyInformation.jsp";
			actionRequest.setAttribute("jspPage", PropertyInformationJSP);
			actionResponse.setRenderParameter("jspPage",
					PropertyInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
					propInfo);
			
			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
					application);

			return;
		}

		if (log.isInfoEnabled()) {
			log.info("successfully retrieved  Property_Information from the web >>> "
					+ propInfo.toString()
					+ "<<< "
					+ propInfo.getApplicationId());
		}
	
		Application_C_of_O applicationTemp = null;
		try {

			if (log.isInfoEnabled()) {
				log.info("successfully saved  Property_Information from the web >>> "
						+ "<<< "
						+ propInfo.getApplicationId());
			}
			// retrieve appl info and do update
			applicationTemp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(application.getApplicationId());

			applicationTemp.setLandUsePurpose(application.getLandUsePurpose());
			applicationTemp.setLandUseType(application.getLandUseType());

			boolean merge = true;
			propInfo = Property_InformationLocalServiceUtil
					.updateProperty_Information(propInfo, merge);
			//heldPlots
			
			for (int i = 0; i <heldPlots.size(); i++) {
				Held_Plots held_plotItem = heldPlots.get(i);
				Held_Plots heldPlot = new Held_PlotsImpl();
				
				heldPlot.setHeld_id(held_plotItem.getHeld_id());
				heldPlot.setApplicationId(held_plotItem.getApplicationId());
				heldPlot.setUserId(held_plotItem.getUserId());
				heldPlot.setLocation(held_plotItem.getLocation());
				heldPlot.setArea(held_plotItem.getArea());
				heldPlot.setDeveloped(held_plotItem.getDeveloped());
				heldPlot.setAcquisitionMode(held_plotItem.getAcquisitionMode());
				heldPlot.setRegParticulars(held_plotItem.getRegParticulars());
			
				held_plotItem = Held_PlotsLocalServiceUtil.updateHeld_Plots(held_plotItem, merge);
				
			
				
			}
			
				
				Application_C_of_OLocalServiceUtil.updateApplication_C_of_O(
					applicationTemp, merge);
			// then commiyt
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.debug("Creating Property_Information >>"
						+ propInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Property_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Property_Information >>"
						+ propInfo.toString() + " <<< failed");
				log.debug("There is a problem persisting Property_Information information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}
		// get the button clicked and behave accordingly
		String buttonClicked = ParamUtil.getString(actionRequest,
				"buttonClicked");
		// System.out.print("Button Clicked Name>>>" + buttonClicked);
		if (buttonClicked.equalsIgnoreCase("btnSave")) {
			String propertyInformationJSP = "/html/application_cofo/stateland/PropertyInformation.jsp";
			actionRequest.setAttribute("jspPage", propertyInformationJSP);
			actionResponse
					.setRenderParameter("jspPage", propertyInformationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
					propInfo);
			
			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
					applicationTemp);

			SessionMessages.add(actionRequest, "property-information-save-success");
			
			return;
		}
		List<Supporting_Document> suppDoc = null;

		actionRequest.setAttribute(
				ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR, propInfo);

		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
				applicationTemp);

		// get the supporting info from db
		try {
			suppDoc = Supporting_DocumentLocalServiceUtil
					.getSupporting_DocumentByApplId(application
							.getApplicationId());

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("New Supporting Documents for >>"
						+ applicationTemp.getApplicationId() + " ");
			}
		}

		if (suppDoc == null || suppDoc.size() == 0) {
			suppDoc = ApplicationActionUtil
					.getSupporting_DocumentInput(applicationTemp);
		}

		actionRequest.setAttribute("suppDoc", suppDoc);
		
		if (log.isInfoEnabled()) {
			log.info("Preparing to navigate to Step 6 of the application >>"
					+ " Supporting Document  page "
					+ propInfo.getApplicationId());
		}

		String supportingDocumentJSP = "/html/application_cofo/common/SupportingDocuments.jsp";
		actionRequest.setAttribute("jspPage", supportingDocumentJSP);
		actionResponse.setRenderParameter("jspPage", supportingDocumentJSP);

	}
	
	
	
	
	public void editSupportingDoc(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		// get the application ID
		String applicationId = ParamUtil.getString(actionRequest,
				"applicationId");

		if (applicationId == null || "".equalsIgnoreCase(applicationId)) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application information failed >>");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			return;
		}

		List<Supporting_Document> suppDocs = null;
		try {
			suppDocs = Supporting_DocumentLocalServiceUtil
					.getSupporting_DocumentByApplId(applicationId);
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application information failed >>"
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");

			e.printStackTrace();

			return;
		}

		actionRequest.setAttribute("suppDoc", suppDocs);

		Application_C_of_O applicationTemp = null;
		try {
			applicationTemp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(applicationId);
		} catch (PortalException e) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application information failed >>"
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");

			e.printStackTrace();

			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application information failed >>"
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");

			e.printStackTrace();

			return;
		}

		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
				applicationTemp);

		String supportingDocumentJSP = "/html/application_cofo/common/SupportingDocuments.jsp";
		actionRequest.setAttribute("jspPage", supportingDocumentJSP);
		actionResponse.setRenderParameter("jspPage", supportingDocumentJSP);

		return;
	}

	/**
	 * This method is evoked on the Step6 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 6 page, otherwise, the page is
	 * forwarded the Step 7 of either Individual or Organisation Application.
	 * 
	 * This method evokes the getSupporting_DocumentFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Supporting_DocumentLocalServiceUtil utility that acts as a facade for
	 * the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void submitSupportingDoc(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		final UploadPortletRequest uploadRequest = PortalUtil
				.getUploadPortletRequest(actionRequest);
		if (log.isDebugEnabled()) {
			log.debug("In  submitSupportingDoc");
		}
		// get the applID to pull the application
		String applicationID = null;

		Application_C_of_O applicationTemp = null;

		try {

			applicationID = ParamUtil.getString(uploadRequest, "applicationId");
			if (applicationID == null)
				throw new PortalException();
			applicationTemp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(applicationID);
		} catch (PortalException e1) {
			if (log.isDebugEnabled()) {
				log.debug("There is problem retrieving Application information for Supporting Docuemnt");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e1.printStackTrace();
			return;
		} catch (SystemException e1) {
			if (log.isDebugEnabled()) {
				log.debug("There is problem retrieving Application information for Supporting Docuemnt");
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e1.printStackTrace();
			return;
		}

		// This is needed to be able to get the names of the supporting document
		// when proper configuration is done, this will go
		List<Supporting_Document> suppDocsWithName = ApplicationActionUtil
				.getSupporting_DocumentInput(applicationTemp);

		// get the newly uploaded supporting document
		List<Supporting_Document> supDocs = ApplicationActionUtil
				.getSupporting_DocumentFromRequest(actionRequest, uploadRequest);

		boolean validationError = false;
		
		List<Supporting_Document> suppError = new ArrayList<Supporting_Document>();
		//loop through each of the downloads and validate for size
		for (Iterator iterator = supDocs.iterator(); iterator.hasNext();) {
			Supporting_Document supporting_Document = (Supporting_Document) iterator
					.next();
			String uploadDoc = supporting_Document.getDocumentUpload();
			if(null != uploadDoc){
				if(uploadDoc.length() >= 540000){
					SessionErrors.add(actionRequest, "file_too_large", "file_too_large" );
					
					
					//we are using the upload property to send the file size
					
					String displayMessage = "File Name:" + supporting_Document.getFileName() + 
							"  File Size:" + uploadDoc.length()/1000 + "Kb." ;
					supporting_Document.setDocumentUpload(displayMessage);
					suppError.add(supporting_Document);
					validationError = true;
					//SessionErrors.add(actionRequest, "no-user-exists-with-the-key", "No User exists with the Email Address");
				}
			}
			
		}
		
		if(validationError){
			
			String supportingDocumentJSP = "/html/application_cofo/common/SupportingDocuments.jsp";
			actionRequest.setAttribute("jspPage", supportingDocumentJSP);
			actionResponse.setRenderParameter("jspPage", supportingDocumentJSP);

			
			applicationTemp.setApplication(applicationID);
			
			actionRequest.setAttribute(ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,applicationTemp );
			
			actionRequest.setAttribute(	"suppError", suppError);
			
			
			actionRequest.setAttribute(	"suppDoc", suppDocsWithName);
			
			return;
			
		}

		if (log.isDebugEnabled()) {
			log.debug("Onochie to do validation");
		}
		// String supportingDocumentJSP =
		// "/html/application_cofo/common/SupportingDocumentjsp";
		// actionRequest.setAttribute("jspPage", supportingDocumentJSP);
		// actionResponse.setRenderParameter("jspPage", supportingDocumentJSP);

		// get and delete the existing documents
		try {
			List<Supporting_Document> oldSuppDocs = Supporting_DocumentLocalServiceUtil
					.getSupporting_DocumentByApplId(applicationID);

			for (Supporting_Document supporting_Document : oldSuppDocs) {
				Supporting_DocumentLocalServiceUtil
						.deleteSupporting_Document(supporting_Document);
				
				//delete the uploaded file
				try {
					DocumentsLocalServiceUtil.
						deleteDocuments(supporting_Document.getDocumentId());
				} catch (PortalException e) {
					if (log.isDebugEnabled()) {
						log.debug("There is a problem getting and delete=ing the existing supporting documents ");
						log.debug("There is a problem persisting Supporting_Document information "
								+ e.getMessage());
					}
				}
				
			}
		} catch (SystemException e1) {
			if (log.isDebugEnabled()) {
				log.debug("There is a problem getting and delete=ing the existing supporting documents ");
				log.debug("There is a problem persisting Supporting_Document information "
						+ e1.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			return;
		}

		for (int i = 0; i < supDocs.size(); i++) {

			Supporting_Document supporting_Document = supDocs.get(i);

			Supporting_Document supporting_DocumentForName = suppDocsWithName
					.get(i);

			// set the document name
			supporting_Document.setDocumentName(supporting_DocumentForName
					.getDocumentName());

			if (log.isInfoEnabled()) {
				log.info("successfully retrieved  Supporting_Document from the web >>> "
						+ supporting_Document.toString()
						+ "<<< "
						+ supporting_Document.getApplicationId());
			}
			try {
				// TODO delete the existing ones and add a fresh
				
				//detach the file portion to another table
				Documents documents = new DocumentsImpl();
				documents.setDocumentId(supporting_Document.getDocumentId());
				documents.setDocumentUpload(supporting_Document.getDocumentUpload());
				documents.setUserId(supporting_Document.getUserId());
				documents.setApplicationId(supporting_Document.getApplicationId());
				
				DocumentsLocalServiceUtil.addDocuments(documents);
				
				supporting_Document.setDocumentUpload("");
				
				//save the actual file information here ...
				
				boolean merge = true;
				supporting_Document = Supporting_DocumentLocalServiceUtil
						.updateSupporting_Document(supporting_Document, merge);
				
				if (log.isInfoEnabled()) {
					log.info("successfully saved  supporting_Document from the web >>> "
							+ supporting_Document.toString()
							+ "<<< "
							+ supporting_Document.getApplicationId());
				}
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Creating Supporting_Document >>"
							+ supporting_Document.toString() + " <<< failed");
					log.debug("There is a problem persisting Supporting_Document information "
							+ e.getMessage());
				}
				// do a page for application error.
				actionRequest.setAttribute("jspPage",
						"/html/application_cofo/ApplicationError.jsp");
				actionResponse.setRenderParameter("jspPage",
						"/html/application_cofo/ApplicationError.jsp");

				// TODO
				// if theere is an error with one of the files, delete the ones
				// that have been saved

				SessionErrors.add(actionRequest, "application-error");
				e.printStackTrace();
				return;
			}

		}

		// restrieve attestation info

		if (log.isInfoEnabled()) {
			log.info("Preparing to navigate to Step 7 of the application >>"
					+ " Attestation page " + applicationTemp.getApplicationId());
		}
		actionRequest.setAttribute(
				ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
				applicationTemp);

		String attestationJSP = "/html/application_cofo/common/Attestation.jsp";
		actionRequest.setAttribute("jspPage", attestationJSP);
		actionResponse.setRenderParameter("jspPage", attestationJSP);

	}

	/**
	 * This method is evoked on the Step7 page ApplicationCofO portlet. If there
	 * are errors, page is forwarded to Step 7 page, otherwise, the page is
	 * forwarded the Confirmation page of either Individual or Organisation
	 * Application.
	 * 
	 * This method evokes the getApplication_C_of_OFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Application_C_of_OLocalServiceUtil utility that acts as a facade for
	 * the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void saveAttestationInformation(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		if (log.isInfoEnabled()) {
			log.info("In  saveAttestationInformation");
		}
		Application_C_of_O appCofO = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);
		if (log.isInfoEnabled()) {
			log.info("successfully retrieved  AttestationInformation from the web >>> "
					+ appCofO.toString() + "<<< " + appCofO.getApplicationId());
		}

		List<String> errors = new ArrayList<String>();
		boolean foundError = ApplicationValidator.validateAttestation(appCofO,
				errors);

		if (foundError) {
			//
			for (String error : errors) {
				if (log.isDebugEnabled()) {
					log.debug("Validation error >>> " + error
							+ " <<<< found for >>> "
							+ appCofO.getApplicationId() + " <<<"
							+ appCofO.getUserId());
				}
				SessionErrors.add(actionRequest, error);
			}

			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, appCofO);

			String attestationJSP = "/html/application_cofo/common/Attestation.jsp";
			actionRequest.setAttribute("jspPage", attestationJSP);
			actionResponse.setRenderParameter("jspPage", attestationJSP);

			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_INFO_SESSION_ATTR, appCofO);

			return;
		}

		Application_C_of_O appCofOTemp = appCofO;
		try {
			if (log.isInfoEnabled()) {
				log.info("successfully retrieved AttestationInformation for update >>> "
						+ appCofO.toString()
						+ "<<< "
						+ appCofO.getApplicationId());
			}
			appCofOTemp = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(appCofO.getApplicationId());

			appCofOTemp.setAttestation(appCofO.getAttestation());

			Application_C_of_OLocalServiceUtil
					.updateApplication_C_of_O(appCofOTemp);
			if (log.isInfoEnabled()) {
				log.info("successfully saved  AttestationInformation  >>> "
						+ appCofO.toString() + "<<< "
						+ appCofO.getApplicationId());
			}

		} catch (PortalException e) {
			if (log.isDebugEnabled()) {
				log.debug("Updating Application >>" + appCofOTemp.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Updating Application >>" + appCofOTemp.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}

		// TODO
		// retrieve all the information that has been saved and put them on the
		// request scope.
		// assume that their is failure
		actionRequest.setAttribute("jspPage",
				"/html/application_cofo/ApplicationError.jsp");
		actionResponse.setRenderParameter("jspPage",
				"/html/application_cofo/ApplicationError.jsp");
		SessionErrors.add(actionRequest, "application-error");

		String applicationType = appCofOTemp.getApplicationType();

		// check application
		boolean retreiveInformation = CofORetrieveUtil.extractApplicationInfoForDisplay(
				actionRequest, appCofOTemp);

		//load Held Plots
		List<Held_Plots> heldPlots = null;
		
		//heldPlots = ApplicationActionUtil.getHeldPlots(actionRequest);
		 DynamicQuery query = DynamicQueryFactoryUtil.forClass(Held_Plots.class)
		   .add(PropertyFactoryUtil.forName("applicationId").eq(new String(appCofO.getApplicationId())));
		 
		 try {
			 heldPlots=	Held_PlotsLocalServiceUtil.dynamicQuery(query);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		actionRequest.setAttribute("heldPlots", heldPlots);
		
		
		// test personal info
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractPersonalInfoForDisplay(
						actionRequest, appCofOTemp);
			} else {
				return;
			}
		}

		// do company information
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractOrganisationInfoForDisplay(
						actionRequest, appCofOTemp);
			} else {
				return;
			}
		}

		// do company contact
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractCompanyContactInfoForDisplay(
						actionRequest, appCofOTemp);
			} else {
				return;
			}
		}

		// test contact info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractContactInfoForDisplay(actionRequest,
					appCofOTemp);
		} else {
			return;
		}

		// application history
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractApplicationHistForDisplay(
					actionRequest, appCofOTemp);
		} else {
			return;
		}

		// test property info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractPropertyInfoForDisplay(actionRequest,
					appCofOTemp);
		} else {
			return;
		}

		// test supporting docs
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractSupportDocForDisplay(actionRequest,
					appCofOTemp);
		} else {
			return;
		}

		log.info("Successfully all the information for display >>> "
				+ appCofOTemp.getApplicationId());

		String applicationSummaryJSP = "";
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			applicationSummaryJSP = "/html/application_cofo/individual/ApplicationCofOsummary.jsp";
		} else {
			applicationSummaryJSP = "/html/application_cofo/organisation/ApplicationCofOsummary.jsp";
		}

		actionRequest.setAttribute("jspPage", applicationSummaryJSP);
		actionResponse.setRenderParameter("jspPage", applicationSummaryJSP);

	}

	/**
	 * This method is evoked on the final Page page ApplicationCofO portlet. If
	 * there are errors, page is forwarded to Step 7 page, otherwise, the page
	 * is forwarded the Confirmation page of either Individual or Organisation
	 * Application.
	 * 
	 * This method evokes the getApplication_C_of_OFromRequest of the Utility
	 * class, does some vaildation, and perists the address information using
	 * the Application_C_of_OLocalServiceUtil utility that acts as a facade for
	 * the Service layer.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void finalSave(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		// TODO change the status of the application .. to what???

		// forward to home landing page ... how can this be done?

		Application_C_of_O appCofO = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);

		// this is retrieved for use in later stage
		Application_C_of_O application = null;
		try {
			application = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(appCofO.getApplicationId());

			Date dateOfApplication = CalendarFactoryUtil.getCalendar()
					.getTime();
			application.setDateOfApplication(dateOfApplication);

			String sNextCounter = "000000"
					+ CounterLocalServiceUtil.increment();
			System.out.println(sNextCounter);

			// DateUtil.getCurrentDate

			SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy");
			Date now = new Date();

			String referenceNo = "CRGIA-"
					+ sdfDate.format(now)
					+ "-"
					+ sNextCounter.substring(sNextCounter.length() - 6,
							sNextCounter.length());

			application.setReferenceNo(referenceNo);

			application
					.setStatus(ApplicationCofOConstants.APPL_C_OF_O_STATUS_SUBMITTED);
			boolean merge = true;
			Application_C_of_OLocalServiceUtil.updateApplication_C_of_O(
					application, merge);
			
			//send mail to the CRGIS for notification of of completed application
			String from ="crgis@crossriverstate.gov.ng";
			String to = "crgis@crossriverstate.gov.ng";
			String bcc=null;
			String subject = "Appication Submitted: " + application.getReferenceNo() ;
			String body ="Please be informed that the above application has been submitted. " +"<br/><p>"+
					"A back office staff is expected to start processing the application." +"<br/><p>"+
					"" +
					"Thank You.";
			
			File[] attachments = null;
			try {
				Emailer.sendEmail(from, to, bcc, subject, body, attachments );
			} catch (Exception e) {
				SessionErrors.add(actionRequest, "problem-sending-mail");
				if(log.isInfoEnabled()){
					log.info(e.getMessage());
				}
				e.printStackTrace();
			}
			
			
			//send a notification mail to the applicant
			
			ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
			.getAttribute(WebKeys.THEME_DISPLAY); 
			
			from = "crgis@crossriverstate.gov.ng";
			to = themeDisplay.getUser().getEmailAddress();
			subject = "Appication Submitted: " + application.getReferenceNo()  ;
			
			String title = themeDisplay.getUser().getLanguageId() + " " + themeDisplay.getUser().getFirstName();
			body = "Dear " + title +"," + "<br/><p>"+
				"Please be informed that your application has been submitted. " +
					"Your application Reference No is: " + application.getReferenceNo() + "<br/><p>"+
					"You will be able to monitor the progress and " +
					"status of you application on this portal. Note that you can also make payments for " +
					"different fees that your application may attarcta s well. " +"<br/><p>"+
					"" +
					"For the list of fees for diffrent transactions, check our website for the details." +
					
					"<br/><p>"+
					"Thank You.";
			
			attachments = null;
			try {
				Emailer.sendEmail(from, to, bcc, subject, body, attachments );
			} catch (Exception e) {
				SessionErrors.add(actionRequest, "problem-sending-mail");
				if(log.isInfoEnabled()){
					log.info(e.getMessage());
				}
				e.printStackTrace();
			}

			//end here
			

		} catch (PortalException e) {
			if (log.isErrorEnabled()) {
				log.debug("Creating Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}

		
		// retrieve all the information that has been saved and put them on the
		// request scope.
		// assume that their is failure
		actionRequest.setAttribute("jspPage",
				"/html/application_cofo/ApplicationError.jsp");
		actionResponse.setRenderParameter("jspPage",
				"/html/application_cofo/ApplicationError.jsp");
		SessionErrors.add(actionRequest, "application-error");

		String applicationType = application.getApplicationType();

		// check application
		boolean retreiveInformation = CofORetrieveUtil.extractApplicationInfoForDisplay(
				actionRequest, application);

		// test personal info
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractPersonalInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// do company information
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractOrganisationInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// do company contact
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractCompanyContactInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// test contact info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractContactInfoForDisplay(actionRequest,
					application);
		} else {
			return;
		}

		// application history
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractApplicationHistForDisplay(
					actionRequest, application);
		} else {
			return;
		}

		// test property info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractPropertyInfoForDisplay(actionRequest,
					application);
		} else {
			return;
		}

		// test supporting docs
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractSupportDocForDisplay(actionRequest,
					application);
		} else {
			return;
		}

		log.info("Successfully all the information for display >>> "
				+ application.getApplicationId());

		String applicationCompleteJSP = "";
	 if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			applicationCompleteJSP = "/html/application_cofo/individual/ApplicationCofOIndComplete.jsp";
		} else {
			applicationCompleteJSP = "/html/application_cofo/organisation/ApplicationCofOOrgComplete.jsp";
		}
		
		actionRequest.setAttribute("jspPage", applicationCompleteJSP);
		actionResponse.setRenderParameter("jspPage", applicationCompleteJSP);
	}

	public void exitApplication(ActionRequest actionRequest,
			ActionResponse actionResponse){
		
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
		.getAttribute(WebKeys.THEME_DISPLAY); 
		long plid = 0;
		try {
			plid = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), true, "/applications").getPlid();
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PortletURL portletURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), 
					"", plid , PortletRequest.RENDER_PHASE);
		
		
		//javax.portlet.action
		portletURL.setParameter("javax.portlet.action", "deleteApplication");
		portletURL.setParameter("show-delete-message", "YES");
		
		try {
			actionResponse.sendRedirect(portletURL.toString()); 
			
			return;
		} catch (IOException e) {
			//there is a problem ridecting to the application landing page

			if(log.isDebugEnabled()){
				log.debug("there is a problem ridecting to the application landing page");
			} 			
			return;
		}
	}

	/**
	 * This method can be evoked before the final save method is called. 
	 * For application, when reuest for delete comes, we are not removing the 
	 * application data from the database. We will tag the application as deleted.
	 * 
	 * 
	 * We are adopting this so that we can analyse requests that are deleted.
	 * Abuse of the system can also be traced with this.
	 * 
	 * One request is tagged as deleted, we exclude it from being seen again
	 * user. How ever the back end users will still be able to see this.
	 * 
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */

	public void deleteApplication(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		Application_C_of_O appCofO = ApplicationActionUtil
		.getApplication_C_of_OFromRequest(actionRequest);

		// this is retrieved for use in later stage
		Application_C_of_O application = null;
		try {
			application = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(appCofO.getApplicationId());
		
		
		
			application
					.setStatus(ApplicationCofOConstants.APPL_C_OF_O_STATUS_DELETED);
			boolean merge = true;
			Application_C_of_OLocalServiceUtil.updateApplication_C_of_O(
					application, merge);
		
		} catch (PortalException e) {
			if (log.isErrorEnabled()) {
				log.debug("Creating Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
		
			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Creating Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem persisting Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
		
			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}


		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
		.getAttribute(WebKeys.THEME_DISPLAY); 
		long plid = 0;
		try {
			plid = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), true, "/applications").getPlid();
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PortletURL portletURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), 
					"", plid , PortletRequest.RENDER_PHASE);
		
		
		//javax.portlet.action
		portletURL.setParameter("javax.portlet.action", "deleteApplication");
		portletURL.setParameter("show-delete-message", "YES");
		
		try {
			actionResponse.sendRedirect(portletURL.toString());
			
			SessionMessages.add(actionRequest, "application-successfully-deleted");
			
			return;
		} catch (IOException e) {
			//there is a problem ridecting to the application landing page

			if(log.isDebugEnabled()){
				log.debug("there is a problem ridecting to the application landing page");
			}
			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, application);

			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/common/ApplicationComplete.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/common/ApplicationComplete.jsp");
			
			e.printStackTrace();
			
			
			
			return;
	
		}
	}

	/**
	 * This is actually doing nothing now.
	 * 
	 */
	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		
		if(log.isInfoEnabled()){
			log.info("in View()");
		}

		HttpServletRequest request = PortalUtil
				.getHttpServletRequest(renderRequest);

		HttpServletRequest origRequest = PortalUtil
				.getOriginalServletRequest(request);

		Enumeration params = origRequest.getParameterNames();

		String queryString = origRequest.getQueryString();

		// this is coming from the menu
		String application = origRequest.getParameter("__application");

		renderRequest.setAttribute("application", application);

		// this is coming from request portlet
		String toPrepend = "_requests_WAR_CRGISTransactionCofOportlet_";
		String fromRequest = origRequest
				.getParameter(toPrepend + "fromRequest");
		if ("yes".equalsIgnoreCase(fromRequest)) {

			System.out.println("fromRequest>>" + fromRequest);
			String methodToCall = origRequest.getParameter(toPrepend
					+ "methodToCall");
			String applicationId = origRequest.getParameter(toPrepend
					+ "applicationId");
			application = origRequest.getParameter(toPrepend + "application");

			// use the information above to direct to the right page
			// added to replace old information by wole
			Application_C_of_O applCofO = new Application_C_of_OImpl();

			applCofO.setApplicationId(applicationId);

			String targetPage = "";

			try {
				applCofO = Application_C_of_OLocalServiceUtil
						.getApplication_C_of_O(applicationId);
			} catch (PortalException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application Information >>"
							+ applicationId + " <<< failed");
					log.debug("There is a problem retrieving Application information "
							+ e.getMessage());
				}

				return;
			} catch (SystemException e) {
				if (log.isDebugEnabled()) {
					log.debug("Retrieving Application Information >>"
							+ applicationId + " <<< failed");
					log.debug("There is a problem retrieving Application information "
							+ e.getMessage());
				}

				return;
			}

			if(log.isInfoEnabled()){
				log.info("Viewing Application ..." +applCofO.toString() );
			}
			
			if ("re-certification".equalsIgnoreCase(applCofO.getApplication())) {
				
				if(log.isInfoEnabled()){
					log.info("Editing re-certification ...");
				}
				
				targetPage = "/html/application_cofo/common/OldFileInformation.jsp";
				
				
				
			} else {

				if(log.isInfoEnabled()){
					log.info("Editing certification ...");
				}
				
				//
				String appType = applCofO.getApplicationType();
				
				if ("Individual".equalsIgnoreCase(appType)){
					Individual_Information indInfo = null;

					try {
						indInfo = Individual_InformationLocalServiceUtil
								.getIndividual_Information(applicationId);
					} catch (PortalException e) {
						if (log.isDebugEnabled()) {
							log.debug("Retrieving Individual Information failed  >>"
									+ applicationId + " ");
						}
						// do a page for application error.
						//e.printStackTrace();
						if(log.isInfoEnabled()){
							log.info("Create a new Individual Info");
						}
						
						indInfo = new Individual_InformationImpl();
						indInfo.setApplicationId(applicationId);

						//return;
					} catch (SystemException e) {
						if (log.isDebugEnabled()) {
							log.debug("Retrieving Individual Information failed  >>"
									+ applicationId + " ");
						}
						// do a page for application error.
						//e.printStackTrace();
						if(log.isInfoEnabled()){
							log.info("Create a new Individual Info");
						}
						
						indInfo = new Individual_InformationImpl();
						indInfo.setApplicationId(applicationId);
					}

					targetPage = "/html/application_cofo/individual/PersonalInformation.jsp";

					renderRequest
							.setAttribute(
									ApplicationCofOConstants.IND_INFO_SESSION_ATTR,
									indInfo);
				}else{
					
					Organisation_Information orgInfo = null;

					try {
						orgInfo = Organisation_InformationLocalServiceUtil
								.getOrganisation_Information(applicationId);
					} catch (PortalException e) {
						if (log.isDebugEnabled()) {
							log.debug("Retrieving Organisation Information failed  >>"
									+ applicationId + " ");
						}
						// do a page for application error.
						//e.printStackTrace();
						if(log.isInfoEnabled()){
							log.info("Create a new Individual Info");
						}
						
						orgInfo = new Organisation_InformationImpl();
						orgInfo.setApplicationId(applicationId);

						//return;
					} catch (SystemException e) {
						if (log.isDebugEnabled()) {
							log.debug("Retrieving Organisation Information failed  >>"
									+ applicationId + " ");
						}
						// do a page for application error.
						//e.printStackTrace();
						if(log.isInfoEnabled()){
							log.info("Create a new Organisation Info");
						}
						
						orgInfo = new Organisation_InformationImpl();
						orgInfo.setApplicationId(applicationId);
					}

					targetPage = "/html/application_cofo/organisation/OrganisationInformation.jsp";

					renderRequest
							.setAttribute(
									ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
									orgInfo);
					
				}
				
			}

			renderRequest
					.setAttribute(
							ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR,
							applCofO);

			this.viewJSP = targetPage;

		} else {
			this.viewJSP = this.getInitParameter("view-jsp");
		}

		// System.out.println(articleId);

		super.doView(renderRequest, renderResponse);

	}

	/**
	 * This method is used to override the addSuccessMessage in the parent
	 * class. This override ensures that success message (that is displayed as
	 * default whenever any request is successfully submitted) is prevented.
	 * 
	 * We like to have control over this and this what this override ensures.
	 * 
	 */
	@Override
	protected void addSuccessMessage(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		if (!addProcessActionSuccessMessage) {
			return;
		}
	}
	@Override
	public void serveResource(
			ResourceRequest resourceRequest, ResourceResponse resourceResponse)
		throws IOException, PortletException {
				resourceRequest.getAttributeNames();
		
		String res = "";
				
		Enumeration<String> params = resourceRequest.getParameterNames();
		while (params.hasMoreElements()) {
			String string = (String) params.nextElement();
			System.out.println(string +">>>" + resourceRequest.getParameter(string));
		}
		
		String queryType= ParamUtil.getString(resourceRequest, "queryType"); // resourceRequest.getParameter("queryType") ;
	
		JSONObject jsonFeed = JSONFactoryUtil.createJSONObject();
			if ("lga".equalsIgnoreCase(queryType)){
				try {
					String stateName =  ParamUtil.getString(resourceRequest, "stateName");  //resourceRequest.getParameter("stateName") ;
					if (stateName.equalsIgnoreCase(stateName)){
						List<ListType> states = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.lga." + stateName );
						
						int stateId = 1;
						for (Iterator iterator = states.iterator(); iterator.hasNext();) {
							
							String sStateId = "param_" + stateId;
							ListType listType = (ListType) iterator.next();
							
							jsonFeed.put(sStateId, listType.getName());
							stateId = stateId + 1;
						}
						
						jsonFeed.put("stateId", stateId-1);
						
					}
					else
					{
						jsonFeed.put("stateId", 0);
					}
						

					
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if ("states".equalsIgnoreCase(queryType))
			{
				try {
					String country = ParamUtil.getString(resourceRequest, "countryName");// resourceRequest.getParameter("countryName") ;
					if ("nigeria".equalsIgnoreCase(country)){
						List<ListType> states = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.state");
						
						int stateId = 1;
						for (Iterator iterator = states.iterator(); iterator.hasNext();) {
							
							String sStateId = "param_" + stateId;
							ListType listType = (ListType) iterator.next();
							
							jsonFeed.put(sStateId, listType.getName());
							stateId = stateId + 1;
						}
						
						jsonFeed.put("stateId", stateId-1);
						
					}
					else
					{
						jsonFeed.put("stateId", 0);
					}
						

					
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			else if ("landUse".equalsIgnoreCase(queryType))
			{
					try {
						String landUse =  ParamUtil.getString(resourceRequest, "landUse");  //resourceRequest.getParameter("stateName") ;
						if (landUse.equalsIgnoreCase(landUse)){
							List<ListType> states = ListTypeServiceUtil.getListTypes("com.teqbridgeltd.liferay.model.land_use." + landUse );
							
							int stateId = 1;
							for (Iterator iterator = states.iterator(); iterator.hasNext();) {
								
								String sStateId = "param_" + stateId;
								ListType listType = (ListType) iterator.next();
								
								jsonFeed.put(sStateId, listType.getName());
								stateId = stateId + 1;
							}
							
							jsonFeed.put("stateId", stateId-1);
							
						}
						else
						{
							jsonFeed.put("stateId", 0);
						}
							

						
					} 
					catch (SystemException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else if("heldPlot".equalsIgnoreCase(queryType)){
					
					//grt the held id
					String held_id =  ParamUtil.getString(resourceRequest, "heldPlotId");
					//delete 
					try {
						Held_PlotsLocalServiceUtil.deleteHeld_Plots(held_id);
					} catch (PortalException e) {
						if(log.isInfoEnabled()){
							log.info(e.getMessage());	
						}
						
					} catch (SystemException e) {
						if(log.isInfoEnabled()){
							log.info(e.getMessage());	
						}
					}
					//
					
				}
	
		res = "'{\"result\":true,\"count\":1}'";
		resourceResponse.setContentType("text/html");
		
		System.out.println(jsonFeed.toString());
		resourceResponse.getWriter().println(jsonFeed.toString());
	}
}
